<?php

namespace Viettrans\FrontBundle\Services;

class MyImageGenerator {
    /**
     * Generate the customizable image from an image
     * @param string $srcPath Absolute path of source image
     * @param string $srcType Type of source image ( gif|jpeg|png )
     * @param string $desPath Absolute path of destination image, if it's empty then return the content of photo
     * @param string $desType Type of destination image ( gif|jpeg|png )
     * @param int $desMaxWidth Width of destination image
     * @param int $desMaxHeight Height of destination image
     * @param int $quality (Default: 80) Quality of destination image ( 1..100 )
     */
    function generate($srcPath, $srcType, $desType, $desMaxWidth, $desMaxHeight, $quality = 80, $desPath = '') {
        # if $srcType || $desType = jpg then we will call the function  imagecreatefromjpg <-- error because we have imagecreatefromjpeg
        # so must to transform type (extension) to string for use in function
        $strSrcTypeForFunction = ($srcType != 'jpg') ? $srcType : 'jpeg';
        $strDesTypeForFunction = ($desType != 'jpg') ? $desType : 'jpeg';
        
        $srcInfos = getimagesize($srcPath);

        # Standardize the image
        $desMaxWidth = ($srcInfos[0] > $desMaxWidth) ? $desMaxWidth : $srcInfos[0];
        $desMaxHeightTmp = $desMaxWidth*($srcInfos[1]/$srcInfos[0]);

        $desMaxHeight = ($desMaxHeightTmp > $desMaxHeight) ? $desMaxHeight : $desMaxHeightTmp;
        $desMaxWidth = $desMaxHeight*($srcInfos[0]/$srcInfos[1]);

        # Must user imagecreatetruecolor instead of imagecreate because imagecreate will return an very ugly image
        if (($desImg = @imagecreatetruecolor($desMaxWidth,$desMaxHeight)) == false) throw new \Exception("Error with function: imagecreatetruecolor\($desMaxWidth,$desMaxHeight\)");
        if (($srcImg =  @call_user_func("imagecreatefrom$strSrcTypeForFunction", $srcPath)) == false) throw new \Exception("Error with function: call_user_func\('imagecreatefrom$strSrcTypeForFunction', $srcPath\)");

        if (!imagecopyresampled($desImg, $srcImg, 0, 0, 0, 0, $desMaxWidth, $desMaxHeight, $srcInfos[0], $srcInfos[1])) throw new \Exception("Error with function: imagecopyresampled\($desImg, $srcImg, 0, 0, 0, 0, $desMaxWidth, $desMaxHeight, ".$srcInfos[0].", ".$srcInfos[1]."\)");

        # Build the image if the dest path is specified
        if ($desPath != '') {
            if (!call_user_func("image$strDesTypeForFunction", $desImg, $desPath, $quality)) throw new \Exception("Error with function: call_user_func\('image$strDesTypeForFunction', $desImg, $desPath, $quality\)");
            if (!imagedestroy($desImg)) throw new \Exception("Error with function: imagedestroy\($desImg\)");
            return true;

        # If des path is not specified then return img data
        } else {
            ob_start();
            if (!call_user_func("image$strDesTypeForFunction", $desImg, '', $quality)) throw new \Exception("Error with function: call_user_func\('image$strDesTypeForFunction', $desImg, '', $quality\)");
            $imgData = ob_get_contents();
            ob_end_clean();
            if (!imagedestroy($desImg)) throw new \Exception("Error with function: imagedestroy\($desImg\)");
            return $imgData;
       }
    }
    
    public function generateFromBase64($srcData, $desFile) {
        if (!file_put_contents($desFile, base64_decode($srcData))) throw new \Exception('Could not generate the image from base64 data.');
        
        return true;
    }
}